//
//  dateView.m
//  dateView
//
//  Created by SC on 16/8/2.
//  Copyright © 2016年 SC. All rights reserved.
//

#import "dateView.h"
#import "NSDate+Compare.h"

@interface dateView (){
    
}

@end

@implementation dateView


-(instancetype)init{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, WIDTH, HEIGHT-64);
        [self setUI];
    }
    return self;
}

-(void)setUI{
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-306)];
    _bgView.backgroundColor = HEXCOLOR(kColorGray1, 0.5);
    _bgView.userInteractionEnabled = YES;
    [self addSubview:_bgView];
    
    UITapGestureRecognizer *bgViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didBgViewtap)];
    [_bgView addGestureRecognizer:bgViewTap];
    
    _bottomView = [[UIView alloc] init];
    _bottomView.frame = CGRectMake(0, HEIGHT-64, WIDTH, 242);
    [UIView animateWithDuration:0.3 animations:^{
        _bottomView.frame = CGRectMake(0, HEIGHT-306, WIDTH, 242);
    }];
    _bottomView.backgroundColor = HEXCOLOR(kColorGray7, 1);
    [self addSubview:_bottomView];

    _sureBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _sureBt.frame = CGRectMake(WIDTH-50, 0, 40, 40);
    //_sureBt.backgroundColor = [UIColor cyanColor];
    [_sureBt setTitle:@"完成" forState:UIControlStateNormal];
    [_sureBt setTitleColor:HEXCOLOR(kColorBlack2, 1) forState:UIControlStateNormal];
    [_sureBt addTarget:self action:@selector(sureClick) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:_sureBt];
    
    _lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 41, WIDTH, 1)];
    _lineView.backgroundColor = HEXCOLOR(kColorGray1, 1);
    [_bottomView addSubview:_lineView];
    
    _datePicker = [[UIDatePicker alloc] init];
    _datePicker.frame = CGRectMake(0, 42, WIDTH, 200);
    _datePicker.datePickerMode = UIDatePickerModeDate;
    _datePicker.backgroundColor = [UIColor whiteColor];
    [_bottomView addSubview:_datePicker];
    
}

-(void)didBgViewtap{
    
    if ([_delegate respondsToSelector:@selector(dateViewDidBgViewEvent)]) {
        [_delegate dateViewDidBgViewEvent];
    }
    
}

-(void)sureClick{
    
    NSDate *saveDate = _datePicker.date;
    
    [[NSUserDefaults standardUserDefaults] setObject:saveDate forKey:@"localDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    NSString *strDate = [NSDate stringFromeDate:saveDate];
    
    if ([_delegate respondsToSelector:@selector(dateViewSureClick:)]) {
        [_delegate dateViewSureClick:strDate];
    }

    
}

-(NSDate *)getSaveDate{
    
    NSDate *localDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"localDate"];
    
    return localDate;
}

@end
